package com.example.courseorder.web;

import com.example.courseorder.student.Course;
import com.example.courseorder.student.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.ArrayList;

@Controller
@RequestMapping("/student")
public class StudentController {

    @GetMapping("/getStudent")
    public String getStudent(Model model){
        Student student = new Student();
        model.addAttribute("studentInfo",student);
        System.out.println(model);
        return "student";
    }
    @PostMapping("/getCourse")
    public String toCheckCourse(@Valid @ModelAttribute("studentInfo")Student student, Errors error, Model model){
        if (error.hasErrors()){
            System.out.println("出错了");
            return "student";
        }
        final ArrayList<Course> courses = new ArrayList<>();
        courses.add(new Course("软件工程"));
        courses.add(new Course("计算机网络"));
        courses.add(new Course("高等代数"));
        courses.add(new Course("大学物理"));
        courses.add(new Course("形式与政策"));
        courses.add(new Course("音乐鉴赏"));
        courses.add(new Course("创新与创业"));
        student.setCourse(courses);
        System.out.println("学生信息为"+student);
        System.out.println(model);
        return "courseInfo";
    }
    @PostMapping("/displayCourse")
    public String showCourseChecked(@Valid @ModelAttribute("studentInfo") Student student, Errors error, Model model){
        System.out.println(student);
        model.addAttribute("coursesChecked",student);
        System.out.println(model);
        return "display";
    }
}
